(function() {
    'use strict';

    angular.module('weApp.controllers')

            //收藏页面
            .controller('favorCtrl', ['$scope', 'myFavorsService', 'showLinePageService', '$ionicPopup', '$rootScope',
                function($scope, myFavorsService, showLinePageService, $ionicPopup, $rootScope) {
                    $scope.lineStation = myFavorsService.getJson().reverse();

                    /**
                     * 打开线路详细页面（直线图页面）点击事件
                     * @param  {[type]} line  [线路JSON Data]
                     * @param  {[type]} event [当前点击的 event 对象]
                     */
                    $scope.showLine = function(line, event) {
                        if (angular.element(event.target).hasClass('station-favor') || angular.element(event.target).hasClass('we-font'))
                            return;
                        showLinePageService.gotoLinePage(line, event);
                    };

                    /**
                     * 下拉刷新
                     * @return {[type]} [description]
                     */
                    $scope.doRefresh = function() {
                        $scope.lineStation = myFavorsService.getJson();
                        $scope.$broadcast('scroll.refreshComplete');
                    };

                    /**
                     * 删除收藏线路
                     * @param  {[type]} line  [description]
                     * @param  {[type]} event [description]
                     * @return {[type]}       [description]
                     */
                    $scope.deleteFovor = function(line) {

                        var confirmPopup = $ionicPopup.confirm({
                            title: '取消收藏的线路',
                            cancelText: '取消',
                            okText: '确认',
                            okType: "button-balanced",
                            template: '取消收藏线路' + line.lineName + ' 吗?'
                        });
                        confirmPopup.then(function(res) {
                            if (res) {
                                myFavorsService.remove(line);
                                // $scope.lineStation = myFavorsService.getJson();
                                $scope.doRefresh();
                            }
                        });

                    };
                }])

})();